package com.cuanbo.dualscreenteachfu.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.widget.AppCompatTextView;

import com.cuanbo.dualscreenteachfu.R;
import com.cuanbo.dualscreenteachfu.utils.CmdUtil;
import com.cuanbo.dualscreenteachfu.utils.SharedPreferencesUtil;

/**
 * 单选按钮
 */
public class CmdRadioView extends AppCompatTextView implements View.OnClickListener {
    private final boolean enablePopupWindow;
    /**
     * 选中时的背景图片id
     */
    protected Drawable checkDrawable;
    /**
     * 未选中时的背景图片id
     */
    protected Drawable unCheckDrawable;
    protected int checkTextColor;
    protected int unCheckTextColor;
    /**
     * 组Id
     */
    protected String groupId;

    public CmdRadioView(Context context) {
        this(context, null);
    }

    public CmdRadioView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CmdRadioView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.cmdradioview, defStyle, 0);
        checkDrawable = a.getDrawable(R.styleable.cmdradioview_checkRadioImg);
        unCheckDrawable = a.getDrawable(R.styleable.cmdradioview_unCheckRadioImg);
        checkTextColor = a.getColor(R.styleable.cmdradioview_checkTextColorRadio, 0xffffffff);
        unCheckTextColor = a.getColor(R.styleable.cmdradioview_unCheckTextColorRadio, 0xffffffff);
        groupId = a.getString(R.styleable.cmdradioview_groupId);
        enablePopupWindow = a.getBoolean(R.styleable.cmdradioview_enableRadioPopuWindow, false);
        init();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        String cmd = SharedPreferencesUtil.getString(getContext(), groupId);
        //读取本地保存的设备状态并更新UI
        if (cmd != null) {
            changeUI(cmd);
        }
    }

    private void init() {
        setOnClickListener(this);
        if(unCheckDrawable!=null) {
            setBackground(unCheckDrawable);
        }
    }

    @Override
    public void onClick(View v) {
        String cmd = (String) getTag();
        executeCMD(cmd);
    }

    public CmdRadioView changeUI(String cmd) {
        if (getParent() == null) {
            return this;
        }
        String tag = (String) getTag();

        if (tag.equals(cmd)) {
            changeUI((ViewGroup) getParent().getParent(), cmd);
        }
        return this;
    }

    public String getGroupId() {
        return groupId;
    }

    /**
     * 置为选中状态
     */
    public void check() {
        setTextColor(checkTextColor);
        if(checkDrawable!=null) {
            setBackground(checkDrawable);
        }
    }

    /**
     * 置为未选中状态
     */
    public void unCheck() {
        setTextColor(unCheckTextColor);
        if(unCheckDrawable!=null){
            setBackground(unCheckDrawable);
        }
    }

    /**
     * 将所有状态置为未选中状态
     */
    protected void changeUI(ViewGroup viewGroup, String cmd) {
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View view = viewGroup.getChildAt(i);
            if (view instanceof CmdRadioView) {
                CmdRadioView cmdTextView = (CmdRadioView) view;
                String strTag = (String) cmdTextView.getTag();
                if (cmdTextView.getGroupId().equals(groupId)) {
                    if (strTag.equals(cmd)) {
                        cmdTextView.check();
                    } else {
                        cmdTextView.unCheck();
                    }
                    SharedPreferencesUtil.save(getContext(), groupId, cmd);
                }
            } else if (view instanceof ViewGroup) {
                changeUI((ViewGroup) view, cmd);
            }
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    /**
     * 保存设备状态
     */
    protected void saveDevState(String cmd) {
        //LogUtil.add("saveDevState", "保存至本地key=" + groupId + " cmd=" + cmd, "CmdRadioView");
        //保存设备状态
        SharedPreferencesUtil.save(getContext(), groupId, cmd);
        //上传设备状态
    }

    private void executeCMD(String cmd) {
        SharedPreferencesUtil.save(getContext(), groupId, cmd);
        CmdUtil.add(cmd);
    }

}
